<?php
class Store_CategoryController extends YouNet_Controller_Action{
	
	/**
	 *
	 * @var int
	 */
	private $_id_store	= 0;
	/**
	 *
	 * @var Core_Model_Store
	 */
	private $_store		= null;
	
	public function preDispatch() {
		parent::preDispatch();
		
		$this->_id_store	= $this->_getParam('id_store', 0);
		$this->_store		= Core_Model_DbTable_Stores::getStoreByUser($this->_id_store, $this->_user);
		$this->view->store	= $this->_store;
		$this->view->render('store/select_fanpages.phtml');
		$this->view->use_admin_menu = true;
        $this->view->new_orders = $this->_store->getNumberNewOrders();
	}
	/**
	* This action is the first view when accessing the app
	*/
	public function indexAction()
	{
		$this->view->messages = $this->_helper->flashMessenger->getMessages();
	}
	
	public function createAction()
	{
		$err = array();
		if( $this->_store->createCategoryFromParams($this->_params,$err) )
		{
			$this->view->messages = array($this->translate('MSG_SAVE_CATEGORY_SUCCESSFUL'));
		}
		else
		{
			if( count($err) )
			{
				$this->view->messages = $err;
			}
			else
			{
				$this->view->messages = array($this->translate('MSG_SAVE_CATEGORY_UNSUCCESSFUL'));
			}
		}
		$this->render('index');
	}
	
	public function editAction()
	{
		$err = array();
		if( $this->_store->updateCategoryFromParams($this->_params, $err) )
		{
			$this->view->messages = array($this->translate('MSG_EDIT_CATEGORY_SUCCESSFUL'));
		}
		else
		{
			if( count($err) )
			{
				$this->view->messages = $err;
			}
			else
			{
				$this->view->messages = array($this->translate('MSG_EDIT_CATEGORY_UNSUCCESSFUL'));
			}
		}
		$this->render('index');
	}
	
	public function moveAction()
	{
		$id		= $this->_getParam('id');
		$to		= $this->_getParam('to');		
		$above	= $this->_getParam('above');
		$this->_store->moveCustomCategory($id, $to, $above);
		$this->disableAutoRender();
	}
	
	public function deleteAction()
	{
		$this->disableAutoRender();
		if( $this->_store->deleteCategoryFromParams($this->_params) )
		{
			$this->_helper->flashMessenger->addMessage($this->translate('MSG_DELETE_CATEGORY_SUCCESSFUL'));
		}
		else
		{
			$this->_helper->flashMessenger->addMessage($this->translate('MSG_DELETE_CATEGORY_UNSUCCESSFUL'));
		}
		$url = $this->view->url(array('action' => 'index','id_category' => NULL));
		Core_Util::redirect($url);
	}
}